/**
 * 隐患上报 编辑页
 *
 * 作者 wanghw
 * 日期 2024-08-20 09:23:04
 */
layui.use(['jquery','febs', 'form', 'validate', 'fileUpload'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        $view = $('#hazardReport-edit'),
        autocomplete = layui.autocomplete,
        commonJS = layui.commonJS,
        planTableIns,
        $unsafeName = $view.find('#unsafeName'),
        fileUpload = layui.fileUpload,
        validate = layui.validate;

    form.verify(validate);
    form.render();
    initFormValue();


    var fileUploadDiv = fileUpload.render({
        elem: '#fileUpload',
        url: ctx + 'hazard/upload',
        type: 0,//0-图片，1-视频，2-其他
        maxNum:5,
        defaultData: hazardReportData && hazardReportData.fileList ? hazardReportData.fileList:[],
        done: function (res) {
        },
    });

    function initFormValue() {
        if (hazardReportData) {
            form.val("hazardReport-edit-form", {
                "reportId": hazardReportData.reportId,
                "tenantId": hazardReportData.tenantId,
                "reportNo": hazardReportData.reportNo,
                "hazardPlace": hazardReportData.hazardPlace,
                // "projectId": hazardReportData.projectId,
                // "projectName": hazardReportData.projectName,
                // "areaId": hazardReportData.areaId,
                // "areaName": hazardReportData.areaName,
                // "projecLeaderId": hazardReportData.projecLeaderId,
                // "projecLeaderName": hazardReportData.projecLeaderName,
                // "hazardOperation": hazardReportData.hazardOperation,
                "hazardDescription": hazardReportData.hazardDescription,
                // "unsafeName": hazardReportData.unsafeName,
            });
            initDetailTale()

        }else {
            form.val("hazardReport-edit-form", {
                "tenantId": POPUP_DATA.tenantId,
            });

            // initProjectAreaId(POPUP_DATA.projectId);
        }
    }

    form.on('submit(hazardReport-edit-form-submit)', function (data) {
        var fileList = getImgData();
        if(commonJS.isEmpty(fileList)){
            febs.alert.warn('隐患图片不能为空！');
            return false;
        }
        data.field.fileList=fileList;
        data.field.status='2';
        addOrUpdate(data.field);
        return false;
    });

    form.on('submit(hazardReport-edit-form-save)', function (data) {
        var fileList = getImgData();
        if(commonJS.isEmpty(fileList)){
            febs.alert.warn('隐患图片不能为空！');
            return false;
        }
        data.field.fileList=fileList;
        data.field.status='1';

        addOrUpdate(data.field);
        return false;
    });

    function addOrUpdate(data) {
        febs.modal.confirm('保存隐患信息', '确定提交信息？', function () {
            if (data.reportId && data.reportId.length > 0) {
                febs.postArray(ctx + 'hazardReport/update', data, function () {
                    layer.closeAll();
                    febs.alert.success('修改成功');
                    $('#febs-hazardReport').find('#query').trigger('click');

                });
            } else {
                febs.postArray(ctx + 'hazardReport', data, function () {
                    layer.closeAll();
                    febs.alert.success('新增成功');
                    $('#febs-hazardReport').find('#query').trigger('click');
                });
            }

        });

    }






    function getImgData(){
        var imgDatas = $view.find(".imgOne")
        var imgList = [];
        $.each(imgDatas,function (index, item){
            var data = {
                filePath: item.dataset.filepath,
                fileName: item.dataset.filename,
                smallPath: item.dataset.smallpath,
            }
            imgList.push(data);
        })
        return imgList;
    }


    function initDetailTale() {
        if (hazardReportData.planRecordList) {
            planTableIns = febs.table.init({
                elem: $view.find('table[id="hazardReportPlanRecordTable"]'),
                id: 'hazardReportPlanRecordTable',
                page: false,
                skin: '',
                limit: Number.MAX_VALUE,
                cols: [
                    [
                        {type: 'numbers', title: '序号'},
                        {field: 'auditRealName', title: '制定人姓名',  width: 120},
                        {
                            field: 'auditResult',
                            title: '制定结果',
                            width: 100,
                            align: 'center',
                            templet: function (d) {
                                if (d.auditResult == '1') {
                                    return '<span class="layui-badge febs-tag-green">通过</span>';
                                } else {
                                    return '<span class="layui-badge febs-tag-magenta">退回</span>';
                                }
                            }
                        },

                        {field: 'auditReason', title: '制定理由', minWidth: 120},
                        {field: 'createdTime', title: '制定时间', width: 160, align: 'center'}
                    ]
                ],
                data: hazardReportData.planRecordList
            });
        }
    }

});